import { message } from "antd"

/** 新建因子 POST /api/addFactor */
export async function addBorrow(options?: { [key: string]: any }) {
  return fetch("/api/borrow", {
    method: "POST",
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(options)
  }).then(res => res.json()).then(res=>{
    if(res.statusCode){
      if(res.statusCode<300 && res.statusCode>=200){
        return {
          success: true
        }
      }else{
        throw new Error(res.message)
      }
    }
    return {
      success: true,
      data:res
    }
  })
}

export async function returnBorrow(options: { [key: string]: any },setOpen:(open:boolean)=>void) {
  return fetch("/api/borrow/return", {
    method: "POST",
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(options)
  }).then(res => res.json()).then(res=>{
    if(res.statusCode){
      if(res.statusCode<300 && res.statusCode>=200){
        return {
          success: true
        }
      }else{
        throw new Error(res.message)
      }
    }
    if(res.success){
      console.log("data:",res.data)
    }else{
      if(res.message === "该图书未被借阅"){
        message.error(res.message)
      }else{
        setOpen(true)
      }
    }
    return {
      success: true,
      data:res
    }
  })
}


export async function forecReturnBorrow(options?: { [key: string]: any }) {
  return fetch("/api/borrow/forceReturn", {
    method: "POST",
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(options)
  }).then(res => res.json()).then(res=>{
    if(res.statusCode){
      if(res.statusCode<300 && res.statusCode>=200){
        if(res.data.success){
          console.log("data:",res.data)
        }
      }else{
        throw new Error(res.message)
      }
    }
    return {
      success: true,
      data:res
    }
  })
}

